package com.lhn.formssi.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lhn.formssi.entity.Customer;
import com.lhn.formssi.service.RedisService;
import com.lhn.formssi.util.CacheFind;
import com.lhn.formssi.util.RedisUtil;
import com.lhn.formssi.util.SystemLogUtil;

@Component  
@Aspect    
public class CacheAop {
	
    @Autowired
    private RedisService redisService;
	
	@Around("@annotation(cacheFind)")
	private Object redisCache(ProceedingJoinPoint proceedingJoinPoint, CacheFind cacheFind) {
		Object result = null;
		Object args[] = proceedingJoinPoint.getArgs();
		RedisUtil ru = new RedisUtil(0, Customer.class);
		String suffix = "";
		for(Object s : args) {
			suffix += ":" + s;
		}
		if(redisService.exists(ru, suffix)) {
			//redis中有缓存
			result = redisService.get(ru, suffix, Customer.class);
			SystemLogUtil.logWriter("redis缓存数据查询----->>>" + result.toString() + "------");
		}else {
			try {
				result = proceedingJoinPoint.proceed();
				redisService.set(ru, suffix, result);
				SystemLogUtil.logWriter("mysql数据查询----->>>" + result.toString() + "------");
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		return result;
	}
}
